<template>
  <div class="list">
    <Header></Header>
    <section>
      <div class="list-l" ref="left">
        <ul class="l-item">
          <li
            :class="{ active: index == currentIndex }"
            v-for="(item, index) in leftData"
            :key="index"
            @click="goScroll(index)"
          >
            {{ item.name }}
          </li>
        </ul>
      </div>
      <div class="list-r" ref="right">
        <div class="r-main">
          <ul v-for="(item, index) in rightData" :key="index" class="shop-list">
            <li v-for="(k, i) in item" :key="i">
              <h2>{{ k.name }}</h2>
              <ul class="r-content">
                <li v-for="(j, idx) in k.list" :key="idx">
                  <img :src="j.imgUrl" alt="" />
                  <span>{{ j.name }}</span>
                </li>
              </ul>
            </li>
          </ul>
        </div>
      </div>
    </section>
    <Tabbar></Tabbar>
  </div>
</template>

<script>
import Tabbar from "@/components/common/Tabbar.vue";
import Header from "@/components/list/Header.vue";
//引入封装后的axiox
import http from "@/common/api/request";
//引入滑动插件
import BetterScroll from "better-scroll";
export default {
  name: "List",
  components: { Tabbar, Header },
  data() {
    return {
      leftData: [], //左侧数据
      rightData: [], //右侧数据
      rightBScroll: "",
      allHeight: [], //每一块的高度
      scrollY: "", //右侧滚动距离
    };
  },
  methods: {
    async getData() {
      let res = await http.$axios({
        url: "/api/goods/list",
      });

      let leftArr = [];
      let rightArr = [];
      res.forEach((v) => {
        leftArr.push({
          id: v.id,
          name: v.name,
        });
        rightArr.push(v.data);
      });

      this.leftData = leftArr;
      this.rightData = rightArr;

      //当dom都加载完毕了再执行
      this.$nextTick(() => {
        new BetterScroll(this.$refs.left, {
          movable: true, //能移动
          zoom: true,
          click: true, //开启点击事件，不然无法触发点击事件，better-Scroll默认关闭
        });

        this.rightBScroll = new BetterScroll(this.$refs.right, {
          movable: true,
          zoom: true,
          click: true,
          probeType: 3, //开启scroll事件
        });

        let height = 0;
        this.allHeight.push(height);

        //获取右侧每块高度
        let uls = this.$refs.right.getElementsByClassName("shop-list");
        //把dom对象转换成功真正的数组
        Array.from(uls).forEach((v) => {
          height += v.clientHeight;
          this.allHeight.push(height);
        });

        this.rightBScroll.on("scroll", (pos) => {
          this.scrollY = Math.abs(pos.y - 100);
        });
      });
    },

    goScroll(index) {
      this.rightBScroll.scrollTo(0, -this.allHeight[index], 300);
    },
  },

  created() {
    this.getData();
  },

  computed: {
    currentIndex() {
      return this.allHeight.findIndex((item, index) => {
        return this.scrollY >= item && this.scrollY < this.allHeight[index + 1];
      });
    },
  },
};
</script>

<style scoped lang="scss">
.list {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

section {
  display: flex;
  flex: 1;
  overflow: hidden;
}

.list-l {
  width: 2.48rem;
  background-color: #fff;
  overflow: hidden;
  border-right: 1px solid #cccccc;
  .l-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    li {
      width: 100%;
      // padding: 0.08rem 0;
      margin: 0.373333rem 0;
      text-align: center;
      font-size: 0.373333rem;
      &.active {
        color: #b54f4a;
        border-left: 5px solid #b54f4a;
      }
    }
  }
}

.list-r {
  flex: 1;
  overflow: hidden;
  .r-main {
    padding-bottom: 10.35rem;
  }
  .shop-list {
    text-align: center;

    h2 {
      font-size: 0.64rem;
      font-weight: 400;
      padding: 0.533333rem 0;
    }

    .r-content {
      display: flex;
      flex-wrap: wrap;
      li {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 33.3%;
        padding: 0.266666rem 0;
        img {
          width: 1.413333rem;
          height: 1.413333rem;
        }
        span {
          font-size: 0.373333rem;
        }
      }
    }
  }
}
::v-deep .tabbar {
  border-top: 1px solid #cccccc;
}
</style>